package com.lry.web.cartoon.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.web.cartoon.entity.CartoonContent;
import com.lry.web.cartoon.mapper.CartoonContentMapper;
import com.lry.web.cartoon.service.CartoonContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class CartoonContentServiceImpl implements CartoonContentService {


    @Autowired
    CartoonContentMapper cartoonContentMapper;



    public Map<String, Object> getCartoonContent(String chapterId) throws IOException {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chapter_id",chapterId);
        List<CartoonContent> f =cartoonContentMapper.selectList(wrapper);

        //对象转换为map
        Map<String, Object> data =new HashMap<String,Object>();
        if(f.size()!=0){
        String strMap = JSONObject.toJSONString(f.get(0));
        JSONObject obj= JSON.parseObject(strMap);
        Iterator it =obj.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            data.put(entry.getKey(), entry.getValue());
        }
        //String转list
        String content = (String) data.get("content");
        List<String> contentToList= Arrays.asList(content.replace("[","").replace("]","").split(","));
        data.put("content",contentToList);
        return data;
        }else{
            data.put("content",new ArrayList<String>());
            return data;
        }

    }

    public int deleteByCartoonId(String cartoonId) throws Exception {
        List<String> listCId = Arrays.asList(cartoonId.split(","));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("cartoon_id",listCId);
        return cartoonContentMapper.delete(wrapper);
    }

    public int deleteByCVId(String cVId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cartoon_variable_id",cVId);
        return cartoonContentMapper.delete(wrapper);
    }

}
